//
//  HomeDataViewController.m
//  Feel_at_home
//
//  Created by dllo on 15/9/28.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "HomeDataViewController.h"
#import "HomeDataView.h"
#import <HTTPTOOL.h>
#import "HomeModel.h"
#import <UIImageView+WebCache.h>
#import "DataBaseHandle.h"
#import "MineModel.h"
@interface HomeDataViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) NSMutableArray *HeadArr;

@property (nonatomic,strong) NSMutableDictionary *HearDic;

@property (nonatomic,strong) NSString *cover_image_urlStr;
@property (nonatomic,strong) NSString *titleStr;
@property (nonatomic,strong) NSString *share_msgStr;

@property (nonatomic,strong) UIWebView *webView;

@property (nonatomic, assign) NSInteger temp;
@property (nonatomic, assign) NSInteger n;
@property (nonatomic, strong) MineModel *mine;

@property (nonatomic, strong) NSString *Urlstr;

@end

@implementation HomeDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.idarr = self.block1(self.arr1);
    NSLog(@"9999 = %@",self.idarr);
    
//    [self tabSubview];
//    [self HandData];
    [self webSubview];
//    [self HandData1];
    
    self.temp = 0;
    if (self.n == 0) {
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(button1Clicked:)];
        
    }else
    {
        
       self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang2.png"] style:UIBarButtonItemStylePlain target:self action:@selector(button1Clicked:)];
        
    }
}

//页面防止加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (self.temp++ == 0) {
        return YES;
    }
    return NO;
}
-(void)webSubview
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.idarr[1]]];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
   
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,-60, self.view.frame.size.width  , self.view.frame.size.height+160)];
    //    self.webView.scalesPageToFit = NO;
    self.webView.scrollView.bounces = NO;
    
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    
    self.webView.delegate = self;
    [self collect];
}
- (void)collect
{
    [[DataBaseHandle  shareDataBase]OpenDB];
    NSArray  *arr = [NSArray  array];
    arr  =  [[DataBaseHandle  shareDataBase]selectAll];
    if(arr.count == 0)
    {
        self.n = 0;
    }
    
    for (MineModel *mine in arr) {
        if ([mine.title isEqualToString:self.idarr[0]]
            ) {
            self.n = 1;
            break;
        }    }
    
    [[DataBaseHandle  shareDataBase]close];
    
}


- (void)button1Clicked:(UIButton *)button
{
    if (self.n == 0) {
        //打开数据库
        [[DataBaseHandle  shareDataBase]OpenDB];
        //创建表格
        [[DataBaseHandle  shareDataBase]createTable];
        
        //插入数据,收藏标题和网址
        self.mine = [[MineModel alloc]init];
        self.mine.title =  self.idarr[0];
        self.mine.url = self.idarr[1];
        
        [[DataBaseHandle shareDataBase]insertCollections:self.mine];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        /* 添加 action */
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:action1];
        /* VC模态推出 alert 界面 */
        [self presentViewController:alert animated:YES completion:^{
        }];
        
        self.n = 1;
        
    }else{
        
        //打开数据库
        [[DataBaseHandle  shareDataBase]OpenDB];
        //删除数据
        [[DataBaseHandle  shareDataBase]deleteCollection:self.idarr[0]];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        /* 添加 action */
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action1];
        /* VC模态推出 alert 界面 */
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        self.n = 0;
    }
    
    [[DataBaseHandle  shareDataBase]close];
    
    if (self.n == 1) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"shoucang2.png"]];
    }else{
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"shoucang1.png"]];
    }
    
}
//
//- (void)tabSubview
//{
//    self.tableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
//    
//    self.tableview.dataSource = self;
//    self.tableview.delegate = self;
//    self.tableview.separatorStyle = NO;
//    self.tableview.backgroundColor = [UIColor clearColor];
//    [self.view addSubview:self.tableview];
//}
//
//- (void)HandData1
//{
//        //获取html的NSData数据
//        NSString *Url =[NSString stringWithFormat:@"http://bohejiaju.liwushuo.com/posts/%@",self.idstr];
//        
//        NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:Url] encoding:NSUTF8StringEncoding error:nil];  //htmlString是html网页的地址
//        NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//    
//        //    NSData *htmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:Url]];
//        // 解析HTML数据
//        TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
//        
//        //根据标签来进行过滤
//        NSArray *dataArray = [xpathParser searchWithXPathQuery:@"//p"];
//         //这个数组中就有需要的值
//        //开始整理数据
//        for (TFHppleElement *hppleElement in dataArray) {
//            //        //打印出该节点所有内容(包括标签)
//            //        NSLog(@"777 == %@",hppleElement.raw);
//            if (dataArray.count != 0) {
//            
//                //打印出该节点所有内容(不包括标签)
//            NSLog(@"888 == %@",hppleElement.text);
//               
//                //过滤制定的数据
////        if ([[hppleElement objectForKey:@"class"]isEqualToString:@"cover"]) {
////                            
////                        NSLog(@"88888 == %@",hppleElement.text);
////                }
//           }
//        }
//    }
//
//- (void)HandData
//{
//    NSString *Url =[NSString stringWithFormat:@"http://api.bohejiaju.com/v1/posts/%@",self.idstr];
//    
//    [HTTPTOOL GETWithURL:Url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
//        
//        self.cover_image_urlStr = [NSString string];
//        self.titleStr = [NSString string];
//        self.share_msgStr = [NSString string];
//        
//        self.HeadArr = [NSMutableArray array];
//        self.HearDic = [NSMutableDictionary dictionary];
//        self.HearDic = [result objectForKey:@"data"];
//        
//        HomeModel *home = [[HomeModel alloc]init];
//        [home setValuesForKeysWithDictionary:self.HearDic];
//        [self.HeadArr addObject:home];
//        
//        self.cover_image_urlStr = home.cover_image_url;
//        self.titleStr = home.title;
//        self.share_msgStr = home.share_msg;
//        
//        [self.tableview reloadData];
//       
//    } fail:^(NSError *error) {
//        
//        
//    }];
//}
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 1;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
//    if (cell == nil) {
//        
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
//    }
//    
//    return cell;
//    
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return 500;
//    }
//    return 0;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//            
//        HomeDataView *view = [[HomeDataView alloc]init];
//        view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//        
//        
//        [view.cover_image_url sd_setImageWithURL:[NSURL URLWithString:self.cover_image_urlStr] placeholderImage:[UIImage imageNamed:@"xinqing.png"]];
//     view.title.text = self.titleStr;
//        
//     view.share_msg.text = self.share_msgStr;
//     
//        
//        [self.tableview addSubview:view];
//            
//        return view;
//    }else{
//    return nil;
//    }
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 200;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
